package person.aoao.library.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import person.aoao.library.entity.WillList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.WillListService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (WillList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:40
 */
@CrossOrigin
@RestController
@RequestMapping("willList")
public class WillListController {
    /**
     * 服务对象
     */
    @Resource
    private WillListService willListService;

    /**
     * 分页查询
     *
     * @param willList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<WillList>> queryByPage(WillList willList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.willListService.queryByPage(willList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<WillList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.willListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param willList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<WillList> add(WillList willList) {
        return ResponseEntity.ok(this.willListService.insert(willList));
    }



    /**
     * 编辑数据
     *
     * @param willList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<WillList> edit(WillList willList) {
        return ResponseEntity.ok(this.willListService.update(willList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.willListService.deleteById(id));
    }


    @GetMapping("isWill/{seatId}/{date}")

    public ResponseData isWill( @PathVariable String seatId,@PathVariable String date){
        List<String> timeIds=this.willListService.isWill(seatId,date);
        return  new ResponseData(ResponseCode.SUCCESS,timeIds);
    }

    @PostMapping("addWill")

    public  ResponseData addWill(@RequestBody WillList list){
        return  new ResponseData(ResponseCode.SUCCESS,this.willListService.insert(list));
    }


    @GetMapping("getWillListByUid/{uid}/{num}/{page}")
    public  ResponseData getWillListByUid(@PathVariable String uid,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.willListService.getWillListByUid(uid,num,page);
        return  data;
    }

    @GetMapping("getWillList/{num}/{page}")
    public ResponseData getWillList(@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.willListService.getWillList(num,page);
        return  data;
    }

    @PostMapping("searchWillList/{state}/{num}/{page}")
    public ResponseData searchWillList(@RequestBody String str,@PathVariable Integer state,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.willListService.searchWillList(str.replace("\"", ""),state,num,page);
        return  data;
    }

    @PostMapping("cancel")
    public ResponseData delete(@RequestBody WillList willList){
        return new ResponseData(ResponseCode.SUCCESS,this.willListService.deleteById(willList.getWillid()));
    }
}

